package mi.payment.transaction.revoke;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.sunmi.pay.hardware.aidl.AidlConstants;

import mi.payment.R;
import mi.payment.constants.Configs;
import mi.payment.db.dao.PayDetail;
import mi.payment.transaction.V2SwingCardActivity;

/**
 * 消费撤销模块 -- 刷卡界面
 */
public class RevokeSwingCardActivity extends V2SwingCardActivity {

    private PayDetail mOriginalPay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        startCheckCard();
    }

    private void initView() {
        mTvTitle.setText(R.string.consume_revoke);

        mOriginalPay = (PayDetail) getIntent().getSerializableExtra("extra_payDetail");
        setAmount(mOriginalPay.amount);
    }

    @Override
    public void onCheckCardSuccess() {
        if (Configs.isRevokeTradeCheckCardNo) { // 判断撤销类卡和交易时是否一致
            PayDetail payDetail = mTradeData.getPayDetail();
            String cardNo = payDetail.getCardNo();
            int cardType = payDetail.getCardType();
            String oriCardNo = mOriginalPay.getCardNo();
            int oriCardType = mOriginalPay.getCardType();
            boolean equalsCardNo = TextUtils.equals(cardNo, oriCardNo);
            boolean equalsCardType = cardType == oriCardType;

            if (equalsCardNo && equalsCardType) {
                RevokeInputPasswordActivity.startAction(this, mOriginalPay);
                finish();
                return;
            }

            if (oriCardType == AidlConstants.CardType.MAGNETIC.getValue() && equalsCardNo) {
                showToast(R.string.card_number_mag);
            } else if (oriCardType == AidlConstants.CardType.NFC.getValue() && equalsCardNo) {
                showToast(R.string.card_number_nfc);
            } else if (oriCardType == AidlConstants.CardType.IC.getValue() && equalsCardNo) {
                showToast(R.string.card_number_ic);
            } else {
                showToast(R.string.card_number_diff);
            }
            startCheckCard();
        } else {
            RevokeInputPasswordActivity.startAction(this, mOriginalPay);
            finish();
        }
    }

    public static void startAction(Context context, PayDetail payDetail) {
        Intent intent = new Intent(context, RevokeSwingCardActivity.class);
        intent.putExtra("extra_payDetail", payDetail);
        context.startActivity(intent);
    }


}
